package com.hqyj.mall.xiaomimall.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class MyUtil {

    //检查是否是合法的手机号码
    public static boolean isMobileNumber(String mobileStr) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(19[0-9])|(147))\\d{8}$");
        Matcher m = p.matcher(mobileStr);
        return m.matches();
    }

    public static boolean isEmail(String email){
         // return ReUtil.isMatch("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$",email);
        //正则表达式匹配任何字符
        String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
        //编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        //检索匹配器对象
        Matcher m = pattern.matcher(email);
        return m.matches();
    }

    //随机生成一个用户名
    public static String generateRandomUsername(){
        return IdUtil.simpleUUID();
    }


    public static String encodePassword(String password){
        return BCrypt.hashpw(password);
        //return null;
    }

    public static boolean validatePassword(String password, String hashPassword){
        return BCrypt.checkpw(password,hashPassword);
    }

    //生成订单编号的算法

    public static String generateOrderId(){
        //XMyyyyMMddHHmmssUUID(16)
        String prefix = "XM";
        String timestamp = DateUtil.format(new Date(),"yyyyMMddHHmmss");
        String uuid = IdUtil.fastSimpleUUID();
        return prefix+timestamp+uuid;
    }


    public static void main(String[] args) {
        //String password = "123456";
        //String password2 = "654321";
        /*
        String hashPassword = MyUtil.encodePassword(password);
        System.out.println(hashPassword);

        if(MyUtil.validatePassword(password2,hashPassword)){
            System.out.println("密码相同");
        }else{
            System.out.println("密码不同...");
        }*/

        /*

        for(int i=0;i<10;i++){
            System.out.println(MyUtil.encodePassword(password));
        }*/

        //System.out.println(MyUtil.generateOrderId());
        for(int i=0;i<1000;i++){
            System.out.println(MyUtil.generateOrderId());
        }
    }
}


